﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.IO;
using Microsoft.Office.Interop.Excel;

namespace uMES.LeanManufacturing.Common
{
   public  class CommonTool
    {
        /// <summary> 
        /// 以流的形式，可以设置很丰富复杂的样式 
        /// </summary> 
        /// <param name="content">Excel中内容(Table格式)</param> 
        /// <param name="filename">文件名</param> 
        /// <param name="cssText">样式内容</param> 
       public static void ExportToExcel(string filename, string content, string cssText)
       {
           var res = HttpContext.Current.Response;
           content = String.Format("<style type='text/css'>{0}</style>{1}", cssText, content);

           res.Clear();
           res.Buffer = true;
           res.Charset = "UTF-8";
           filename = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.GetEncoding(65001).GetBytes(Path.GetFileName(filename)));
           res.AddHeader("Content-Disposition", "attachment; filename=" + filename);
           res.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
           res.ContentType = "application/ms-excel;charset=UTF-8";
          // res.ContentType = "application/octet-stream";
           res.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
           res.Write(content);
           res.Flush();
           res.End();
       }
       /// <summary>
       /// 根据数据源datatable生成表格
       /// </summary>
       /// <param name="dt"></param>
       /// <param name="strTitle"></param>
       /// <returns></returns>
       public static string getExcelContent(System.Data.DataTable dt, string strTitle)
       {
           StringBuilder sb = new StringBuilder();

           sb.Append("<table borderColor='black' border='1'>");
           sb.AppendFormat("<thead><tr><th colSpan='{0}' bgColor='#ccfefe'>", dt.Columns.Count);
           sb.AppendFormat("{0}", strTitle);
           sb.Append("</th></tr><tr>");
           foreach (DataColumn dc in dt.Columns)
           {
               sb.AppendFormat("<th bgColor='#ccfefe'>{0}</th>", dc.Caption);
           }
           sb.Append("</tr></thead>");
           sb.Append("<tbody>");
           foreach (DataRow dr in dt.Rows)
           {
               sb.Append("<tr>");
               foreach (object str in dr.ItemArray)
               { 
                   if (str.GetType().Name == "String")
                   {
                       sb.AppendFormat("<td style='text-align:center;'>{0}</td>", str.ToString());
                   }
                   else if (str.GetType().Name == "Decimal")
                   {
                       sb.AppendFormat("<td>{0:D2}</td>", str.ToString());
                   }
                   else if (str.GetType().Name == "Double")
                   {
                       sb.AppendFormat("<td>{0:D2}</td>", str.ToString());
                   }
                   else if (str.GetType().Name == "Int32")
                   {
                       sb.AppendFormat("<td>{0}</td>", str.ToString());
                   }
                   else if (str.GetType().Name == "DateTime")
                   {
                       sb.AppendFormat("<td>{0}</td>", str.ToString());
                   }
                   else if (str.GetType().Name == "DBNull")
                   {
                       sb.AppendFormat("<td></td>");
                   }
                   else
                   {
                       sb.AppendFormat("<td>{0}</td>", str.ToString());
                   }
               }
               sb.Append("</tr>");
           }
           sb.Append("</tbody></table>");
           return sb.ToString();
       }
    }
}
